package com.woniuxy.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.WarehouseStyle;
import com.woniuxy.service.WarehouseStyleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/warehousestyle")
@Api(tags = "仓库添加仓库分类接口")
public class WarehouseStyleController {
    @Autowired
    WarehouseStyleService warehouseStyleService;

    @GetMapping("/findAll/{currentPage}/{pageSize}")
    @ApiOperation(value = "仓库分类分页详情")
    public ResponseEntity findAll(@PathVariable int currentPage, @PathVariable int pageSize) {
        return warehouseStyleService.findAll(currentPage, pageSize);
    }

    @GetMapping("/findByName/{currentPage}/{pageSize}/{name}")
    @ApiOperation(value = "分类模糊查询")
    public ResponseEntity findByName(@PathVariable int currentPage, @PathVariable int pageSize, @PathVariable String name) {
        String decode = URLDecoder.decode(name);
        ResponseEntity all = warehouseStyleService.findByName(currentPage, pageSize, decode);
        PageInfo info = new PageInfo<WarehouseStyle>((List<WarehouseStyle>) all.getData());
        all.setData(info);
        return all;
    }

    @PostMapping("/addOne")
    @ApiOperation(value = "新增一个仓库分类")
    public ResponseEntity addOne(@ApiParam(name = "warehousestyle", value = "新增的仓库分类") @RequestBody WarehouseStyle warehouseStyle) {
        return warehouseStyleService.addOne(warehouseStyle);
    }

    @PutMapping("/deleteOne/{id}")
    @ApiOperation(value = "删除一个仓库分类")
    public ResponseEntity deleteOne(@ApiParam(name = "id", value = "仓库分类编码") @PathVariable String id) {
        return warehouseStyleService.deleteOne(id);
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改一个仓库分类")
    public ResponseEntity updateOne(@ApiParam(name = "id", value = "仓库分类编码") @RequestBody WarehouseStyle warehouseStyle) {
        return warehouseStyleService.updateOne(warehouseStyle);
    }

    @PutMapping("/deleteSome")
    @ApiOperation(value = "批量删除仓库分类")
    public ResponseEntity deleteSome(@ApiParam(name = "id", value = "仓库分类id") @RequestBody int[] id) {
        return warehouseStyleService.deleteSome(id);
    }

    @GetMapping("/findByCode/{style_code}")
    @ApiOperation(value = "根据分类编码查找")
    public ResponseEntity findByCode(@ApiParam(name = "style_code", value = "分类编码") @PathVariable String style_code) {
        return warehouseStyleService.findByCode(style_code);
    }
    @GetMapping("/findByUname/{style_name}")
    @ApiOperation(value = "根据分类名称查找")
    public ResponseEntity findByUname(@ApiParam(name = "style_name", value = "分类名称") @PathVariable String style_name){
        String newName = null;
        try {
            newName = URLDecoder.decode(style_name, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return warehouseStyleService.findByUname(newName);
    }
}
